package ch.ethz.amerkle.sender;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;

public class SmsSender {
	public static boolean dev = false;
	private static String url = "https://www.sms.ethz.ch/cgi-bin/sms/send.pl";
	
	private String user = null;
	private String pw = null;
	
	private static String ACTION_LISTADDRESSBOOK 	= "listaddressbook";
	private static String ACTION_SENDSMS 			= "sendsms";
	private static String ACTION_LISTORIGINATOR		= "listoriginators";
	private static String ACTION_DELETEADDRESS		= "deleteaddress";
	private static String ACTION_ADDADDRESS			= "addaddress";
	private static String ACTION_MODIFIYADDRESS		= "modifyaddress";
	
	private static String PARAM_ACTION				= "action";
	private static String PARAM_USERNAME			= "username";
	private static String PARAM_PASSWORD			= "password";
	private static String PARAM_MESSAGE				= "message";
	private static String PARAM_NUMBER				= "number";
	private static String PARAM_ORIGINATOR			= "originator";
	private static String PARAM_NAME				= "name";
	private static String PARAM_ROWID				= "rowid";
	
	private static String ORIGINATOR_PHONE = "phone";
	private static String ORIGINATOR_AUTO  = "auto";
	
	private String statusString = null;
	private int statusCode;

	public SmsSender(String user, String pass){
		System.err.close();
		this.user = user;
		this.pw = pass;
	}
	
	public int getStatusCode(){
		return statusCode;
	}
	
	public String getStatusString(){
		return statusString;
	}
	
	public ArrayList<AddressBookEntry> listAddressBook(){
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(url);
		BufferedReader br = null;
		String line;
		try{
			method.addParameter(PARAM_ACTION, ACTION_LISTADDRESSBOOK);
			method.addParameter(PARAM_USERNAME, user);
			method.addParameter(PARAM_PASSWORD, pw);
			
			client.executeMethod(method);
			if(dev)
				System.out.println(method.getResponseBodyAsString());
			br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
			
			String[] values;
			ArrayList<AddressBookEntry> addresses = new ArrayList<AddressBookEntry>();
			line = br.readLine();
			this.statusCode = method.getStatusCode();
			this.statusString = method.getStatusText();
			if(this.statusCode != 200) return null;

			while((line = br.readLine())!= null){
				line = line.replaceAll("\"", "");
				values = line.split(",");
				addresses.add(new AddressBookEntry(values[0], values[1], values[2], Integer.parseInt(values[3])));
			}
			
			
			
			return addresses;
		} catch (Exception e) {
			if(dev)
				e.printStackTrace();
		}
		return null;
	}
	
	public boolean sendSms(String message, String number, boolean originatorPhone){
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(url);
		
		try{
			method.addParameter(PARAM_ACTION, ACTION_SENDSMS);
			method.addParameter(PARAM_USERNAME, user);
			method.addParameter(PARAM_PASSWORD, pw);
			method.addParameter(PARAM_MESSAGE, message);
			method.addParameter(PARAM_NUMBER, number);
			if(originatorPhone)
				method.addParameter(PARAM_ORIGINATOR, ORIGINATOR_PHONE);
			else
				method.addParameter(PARAM_ORIGINATOR, ORIGINATOR_AUTO);
			
			
			client.executeMethod(method);
			byte[] responseBody = method.getResponseBody();
			this.statusCode = method.getStatusCode();
			this.statusString = method.getStatusText();
			
			if(dev)
				System.out.println(new String(responseBody));
			if(this.statusCode != 200){
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			if(dev)
				e.printStackTrace();
		}
		return false;
	}
	
	public boolean addAddress(String name, String number){
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(url);
		
		try{
			method.addParameter(PARAM_ACTION, ACTION_ADDADDRESS);
			method.addParameter(PARAM_USERNAME, user);
			method.addParameter(PARAM_PASSWORD, pw);
			method.addParameter(PARAM_NAME, name);
			method.addParameter(PARAM_NUMBER, number);
			
			client.executeMethod(method);
			byte[] responseBody = method.getResponseBody();
			this.statusCode = method.getStatusCode();
			this.statusString = method.getStatusText();
			
			if(dev)
				System.out.println(new String(responseBody));
			if(this.statusCode != 200){
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			if(dev)
				e.printStackTrace();
		}
		return false;
	}
	
	public boolean modifyaddress(String id, String name, String number){
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(url);
		
		try{
			method.addParameter(PARAM_ACTION, ACTION_MODIFIYADDRESS);
			method.addParameter(PARAM_USERNAME, user);
			method.addParameter(PARAM_PASSWORD, pw);
			method.addParameter(PARAM_ROWID, id);
			if(name != null && name.length()>0)
				method.addParameter(PARAM_NAME, name);
			if(number != null && number.length()>0)
				method.addParameter(PARAM_NUMBER, number);
			
			client.executeMethod(method);
			byte[] responseBody = method.getResponseBody();
			this.statusCode = method.getStatusCode();
			this.statusString = method.getStatusText();
			
			if(dev)
				System.out.println(new String(responseBody));
			if(this.statusCode != 200){
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			if(dev)
				e.printStackTrace();
		}
		return false;
	}

	public boolean deleteaddress(String id){
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(url);
		
		try{
			method.addParameter(PARAM_ACTION, ACTION_DELETEADDRESS);
			method.addParameter(PARAM_USERNAME, user);
			method.addParameter(PARAM_PASSWORD, pw);
			method.addParameter(PARAM_ROWID, id);
			
			client.executeMethod(method);
			byte[] responseBody = method.getResponseBody();
			this.statusCode = method.getStatusCode();
			this.statusString = method.getStatusText();
			
			if(dev)
				System.out.println(new String(responseBody));
			if(this.statusCode != 200){
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			if(dev)
				e.printStackTrace();
		}
		return false;
	}
}
